package com.zhongxi.guoyuan.fragment.hmfragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoin.ShopNewCateAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/10.
 */
@SuppressLint("ValidFragment")
public class AllMFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2<ListView> {

    @Bind(R.id.tv0)
    TextView tv0;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshLv;
    private List<HotGoodsData> data = new ArrayList<>();
    private ShopNewCateAdapter adapter;
    private String trim;
    private String id="";

    public AllMFragment() {
        super();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new_cate, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        super.initView();
        pullToRefreshLv.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new ShopNewCateAdapter(context, data);
        pullToRefreshLv.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        super.initData();
//        TextView tv = new TextView(context);
//        tv.setGravity(Gravity.CENTER);
//        tv.setText("暂无数据,敬请期待");
//        tv.setTextSize(20);
//        //当界面为空的时候显示的视图
//        pullToRefreshLv.setEmptyView(tv);
//        getList(id);
    }

    private void getList(String id) {
        String str = "&cateid="+id + "&goodstype=0" + "&page=" + pageNum;
        getServer(Url.MALL_GOODS_LIST, null, str, false, null, "get");
    }

    private void getPostGoodsList(String id,String trim) {
        Map<String, String> map = new HashMap<>();
        map.put("content", trim);
        map.put("shopid", "");
        map.put("cateid", id);
        map.put("goodstype", "0");
        String str = "&page=" + pageNum;
        getServer(Url.MALL_GOOD_SEARCH, map, str, false, null, "post");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        pullToRefreshLv.setOnRefreshListener(this);
        pullToRefreshLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent(context, RewardCoinDetailActivity.class);
                intent.putExtra("gid", data.get(position - 1).getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            HotGoodsResult result = gson.fromJson(response, HotGoodsResult.class);
            if (result.getSafeobj() != null && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (pageNum == 1) {
                data.clear();
            }
            if (null != result.getData() && result.getData().size() > 0) {
                data.addAll(result.getData());
            } else {
                if (pageNum > 1) {
                    pageNum--;
                }
            }
            adapter.notifyDataSetChanged();
            pullToRefreshLv.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            pullToRefreshLv.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;

        if (TextUtils.isEmpty(trim)) {
            getList(id);
        } else {
            getPostGoodsList(id,trim);
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        if (TextUtils.isEmpty(trim)) {
            getList(id);
        } else {
            getPostGoodsList(id,trim);
        }
    }

    public void cleanList() {
        data.clear();
        adapter.notifyDataSetChanged();
    }

    public void setPrimary(String id, String trim) {
        this.id = id;
        this.trim = trim;
        pageNum = 1;
        if (TextUtils.isEmpty(trim)) {
            getList(id);
        } else {
            getPostGoodsList(id,trim);
        }
    }
}
